﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SK3000RunningTestTool.SKService;
using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKServerDelegate;
using SK3000RunningTestTool.SKServerSource;

namespace SK3000RunningTestTool.CustomControl
{
    public partial class ServiceTestStatusControl : UserControl,ISKService
    {
        public SKServer Server
        {
            get;
            set;
        }
        public int ID
        {
            get => service.ID;
            set => service.ID = value;
        }
        private ISKService service;
        /// <summary>
        /// 类型ID
        /// </summary>
        public Enum_ServiceTestType TypeID
        {
            get { return service.TypeID; }
            set { service.TypeID = value; }
        }

        public ServiceTestType Type => service.Type;

        public event ServiceTestProgressDelegate ServiceTestProgressEvent;

        private void ServiceTestProgress(ServiceTestProgressValue value)
        {
            if (ServiceTestProgressEvent != null)
            {
                ServiceTestProgressEvent(this, value);
            }
        }



        public event ServiceTestCallbackDelegate ServiceTestCallbackEvent;

        public void ServiceTestCallback(ServiceTestCallbackValue value)
        {
            if (ServiceTestCallbackEvent != null)
            {
                ServiceTestCallbackEvent(this, value);
            }
        }

        public ISKService Service
        {
            get { return service; }
            set
            {
                SetService(value);
            }
        }

        public string ServiceName
        {
            get => service.ServiceName;
            set => service.ServiceName = value;
        }

        public string ServiceRemark
        {
            get;
            set;
        }
        public string ServiceAddress
        {
            get => service.ServiceName;
            set => service.ServiceName = value;
        }
        public ushort ServicePort
        {
            get => service.ServicePort;
            set => service.ServicePort = value;
        }

        public ServiceTestStatusControl()
        {
            InitializeComponent();
        }

        public ServiceTestStatusControl(ISKService iservice)
        {
            InitializeComponent();
        }

        public bool SetService(ISKService iservice)
        {
            bool bolResult = false;
            service = iservice;
            if (service != null)
            {
                service.ServiceTestProgressEvent -= Test_ServiceTestProgressEvent;
                service.ServiceTestCallbackEvent -= Test_ServiceTestCallbackEvent;
                service.ServiceTestProgressEvent += Test_ServiceTestProgressEvent;
                service.ServiceTestCallbackEvent += Test_ServiceTestCallbackEvent;
                groupBox1.Text = service.ServiceName + "[" + service.ServiceRemark + "]";
            }
            return bolResult;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            
            service.ServiceTestAsync(new ServiceTestCompleteDelegate(ServiceTestComplete));
        }

        private void Test_ServiceTestCallbackEvent(object sender, ServiceTestCallbackValue ServiceTestCallbackValue)
        {
            ISKService test = (ISKService)sender;
            this.BeginInvoke(new EventHandler(delegate
            {
                lblPrompt.Text = ServiceTestCallbackValue.EventValue.ToString();
            }));
            ServiceTestCallback(ServiceTestCallbackValue);
            Console.WriteLine(DateTime.Now.ToString() + "执行测试结束，结果:" + ServiceTestCallbackValue.EventValue.ToString());
        }
        public void ServiceTestComplete(object sender, ServiceTestCompleteValue valuess)
        {
            ISKService test = (ISKService)sender;
            string strPormpt = "";
            switch (valuess.reuslt)
            {
                case Enum_ServiceTestResult.Normal:
                    strPormpt = "[服务正常]";
                    break;
                case Enum_ServiceTestResult.Fail:
                    strPormpt = "[服务异常]" + Convert.ToString(valuess.Para1);
                    break;
                case Enum_ServiceTestResult.TestException:
                    strPormpt = "[服务异常]" + Convert.ToString(valuess.Para1);
                    break;
                default:
                    break;
            }

            this.BeginInvoke(new EventHandler(delegate
            {
                lblPrompt.Text = strPormpt;
            }));
            Console.WriteLine(DateTime.Now.ToString() + "执行测试结束，结果:" + valuess.reuslt.ToString());
        }

        private void Test_ServiceTestProgressEvent(object sender, ServiceTestProgressValue ServiceTestCompleteValue)
        {
            Console.WriteLine(DateTime.Now.ToString() + "执行测试结束，进度:" + ServiceTestCompleteValue.ProgressValue.ToString());
            ISKService test = (ISKService)sender;
            this.BeginInvoke(new EventHandler(delegate
            {
                progressBar.Value = ServiceTestCompleteValue.ProgressValue;
            }));
            ServiceTestProgress(ServiceTestCompleteValue);
        }

        public ServiceTestCompleteValue ServiceTest()
        {
            return service.ServiceTest();
        }

        public void ServiceTestAsync(ServiceTestCompleteDelegate callback)
        {
            ServiceTestCompleteDelegate Temp;
            if (callback == null)
            {
                Temp = new ServiceTestCompleteDelegate(ServiceTestComplete);
            }
            else
            {
                Temp = new ServiceTestCompleteDelegate(callback);
            }
            service.ServiceTestAsync(new ServiceTestCompleteDelegate(Temp));
        }
    }
}
